$(function() {
    $(".addtodolist").click(function() {
        if (typeof(Storage) !== "undefined")
        {
            if (checkValidate('error-msg')) {
                $(".addtodolist").attr("disabled", "disabled");
                if ($(this).hasClass("add-todo")) {
                    todo_edit_id = 0;
                } else {
                    todo_edit_id = sessionStorage.todo_edit_id;
                }

                $.post(localStorage.domain_app+"/webservice/toDo/updateToDo.php", {
                    todo_item: $('input[name="todo-item"]').val(),
                    todo_date: $('input[name="todo-date"]').val(),
                    start_time: $('input[name="start-time"]').val(),
                    end_time: $('input[name="end-time"]').val(),
                    user_id: localStorage.userID,
                    todo_id: todo_edit_id,
                }, function(res) {
                    var msgEr = '';
                    var num_milliseconds = 3000;
                    if (res.error == 1) {
                        msgEr = res.msg;

                    } else {
                        print_alert(res.msg);
                    }
                    if (msgEr === '') {
                        wd_redirect("toDoList.html");

                    } else {
                        $('#error-msg').addClass('active').children().html(msgEr);
                        hideError_2('error-msg', num_milliseconds);

                    }
                    $(".addtodolist").removeAttr("disabled");
                }, "json");
            }
        } else {
            return false;
        }
    });

    $('.input_text').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
           
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });


    $('.todo_start_time').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val('').prop('type', 'time');
           
            this.select();

        }
    }).blur(function() {

        if (jQuery.trim(this.value) == '') {
            $(this).prop('type', 'text');
            this.value = (this.defaultValue ? this.defaultValue : '');
            

        }
    });

    $('.todo_end_time').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {

            $(this).val('').prop('type', 'time');
            
            this.select();

        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {

            $(this).prop('type', 'text');
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });

    $('.todo_date').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == "Date (mm/dd/yy)*") {

            $(this).val('').attr('type', 'date');
            
            this.select();

        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = "Date (mm/dd/yy)*";
            $(this).attr('type', 'text');
        }
    });
});
function checkValidate(id_append) {

    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please enter %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    //check
    if ($('input[name="todo-item"]').val() == '' || $('input[name="todo-item"]').val() == 'To-Do Item*') {
        msgEr = errormsg[2].replace('%s', 'task description');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="todo-item"]'), num_milliseconds);
        return false;
    }

    if ($('input[name="todo-date"]').val() == '' || $('input[name="todo-date"]').val() == 'Date (mm/dd/yy)*') {
        msgEr = errormsg[2].replace('%s', 'task’s date');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="todo-date"]'), num_milliseconds);
        return false;
    }
   
    //start time
    if ($('input[name="start-time"]').val() == '' || $('input[name="start-time"]').val() == 'Start Time*') {
        msgEr = errormsg[2].replace('%s', 'task’s start time');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="start-time"]'), num_milliseconds);
        return false;
    }
   
    //end time
    if ($('input[name="end-time"]').val() == '' || $('input[name="end-time"]').val() == 'End Time*') {
        msgEr = errormsg[2].replace('%s', 'task’s end time');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="end-time"]'), num_milliseconds);
        return false;
    }
   
    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
//            $('#'+id_append+'').show();
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError_2(id_append, num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function isLengthBetween(str, min, max) {
    if (max) {
        return (str.length >= min) && (str.length <= max);
    } else {
        return (str.length >= min);
    }
}
function saveToDoID() {
    if (typeof(Storage) !== "undefined")
    {
        $(".wd-edit-guest").click(function() {
            sessionStorage.todo_edit_id = $(this).attr('rel');
            return true;
        });

    }
    else
    {
        return false;
    }


}
function actionDeleteToDo(button, todo_id) {
    if (button == 1) {
        var user_id = 0;
        if (typeof(Storage) !== "undefined")
        {
            user_id = localStorage.userID;
        }
        $.post(localStorage.domain_app+"/webservice/toDo/deleteToDo.php", {
            todo_id: todo_id,
            user_id: user_id
        }, function(res) {
             btn_delete = false;
            var num_milliseconds = 3000;
            if (res.error == 1) {
                $('#error-msg').addClass('active').children().html(res.msg);
                hideError_2('error-msg', num_milliseconds);

            } else {
                wd_redirect("toDoList.html");
            }
        }, "json");
    } else {
         btn_delete = false;
        return false;
    }
}
 var btn_delete = false;
function deleteToDo() {
    //delete guest
   
    $(".wd-delete-guest").click(function() {

        if (btn_delete == false) {
            btn_delete = true;
            var todo_id = $(this).attr('rel');
            navigator.notification.confirm(
                    "Are you sure you want to delete this task?",
                    function(buttonIndex) {
                        actionDeleteToDo(buttonIndex, todo_id);
                    }
            , // callback to invoke with index of button pressed
                    'Wedding Helper', // title
                    'OK,Cancel'          // buttonLabels
                    );

        }
       

    });
}

